/*
 * Recurso_Humano.java
 *
 * Created on 19 de abril de 2008, 09:23 PM
 */

package GUI;
import javax.swing.*;
import javax.swing.table.*;
import AccesoDatos.*;
import ReglasNegocio.*;
import org.jdesktop.swingx.autocomplete.*;
/**
 *
 * @author  Fermin
 */
public class frmRecursoHumano extends javax.swing.JFrame {
    
    /** Creates new form Recurso_Humano */
    public frmRecursoHumano() {
        initComponents();
        this.setLocationRelativeTo(null);
        this.comboFincas.setModel(this.modeloFincas());
        AutoCompleteDecorator.decorate(comboFincas);
        this.comboJerarquia.setModel(this.modeloJerarquia());
        AutoCompleteDecorator.decorate(this.comboJerarquia);
        this.listaTelefono.setModel(new DefaultListModel());
    }
    
    private DefaultComboBoxModel modeloJerarquia() {
        DefaultComboBoxModel modelo=new DefaultComboBoxModel();
        String buscar="select * from jerarquia";
        ConMySQL con=new ConMySQL();
        con.Conectar();
        Vista v=con.crearVista(buscar);
        if (v.getNRegistros()>0) {
            for(int i=0;i<v.getNRegistros();i++) {
                int id=(Integer)v.getRegistro(i, 0);
                Jerarquia j=new Jerarquia(new Dato("nivel",String.valueOf(id)));
                modelo.addElement(j);
            }
        }
        return modelo;
    }
    
    private DefaultComboBoxModel modeloFincas() {
        DefaultComboBoxModel modelo=new DefaultComboBoxModel();
        ConMySQL con=new ConMySQL();
        con.Conectar();
        String buscar="select * from finca";
        Vista v=con.crearVista(buscar);
        if (v.getNRegistros()>0) {
            for(int i=0;i<v.getNRegistros();i++) {
                long id=(Long) v.getRegistro(i, 0);
                Dato idf=new Dato("id",String.valueOf(id));
                Finca f=new Finca(idf);
                modelo.addElement(f);
            }
        }
        con.Cerrar();
        return modelo;
    }
    
    public static void main(String[] args) {
        new frmRecursoHumano().setVisible(true);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        PopupMenu = new javax.swing.JPopupMenu();
        ItemEliminar = new javax.swing.JMenuItem();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        OpMandador = new javax.swing.JRadioButton();
        OpGerente = new javax.swing.JRadioButton();
        jPanel2 = new javax.swing.JPanel();
        txtPseudonimo = new javax.swing.JTextField();
        txtNombres = new javax.swing.JTextField();
        txtApellidos = new javax.swing.JTextField();
        comboFincas = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        comboJerarquia = new javax.swing.JComboBox();
        btnAgregarTelefono = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listaTelefono = new javax.swing.JList();
        btnRegistrar = new javax.swing.JButton();

        ItemEliminar.setMnemonic('Q');
        ItemEliminar.setText("Quitar");
        ItemEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ItemEliminarActionPerformed(evt);
            }
        });
        PopupMenu.add(ItemEliminar);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel1.setText("RECURSO  HUMANO");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Atributos Persona"));

        buttonGroup1.add(OpMandador);
        OpMandador.setSelected(true);
        OpMandador.setText("Mandador");

        buttonGroup1.add(OpGerente);
        OpGerente.setText("Gerente");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(OpGerente)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                .addComponent(OpMandador)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(OpGerente)
                    .addComponent(OpMandador))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos"));

        jLabel6.setText("Nivel");

        jLabel4.setText("Finca");

        jLabel3.setText("Apellidos");

        jLabel2.setText("Nombres");

        jLabel5.setText("Pseudonimo");

        btnAgregarTelefono.setMnemonic('A');
        btnAgregarTelefono.setText("Agregar Telefono");
        btnAgregarTelefono.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarTelefonoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 223, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel6))
                        .addGap(62, 62, 62)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtNombres, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
                            .addComponent(txtPseudonimo, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
                            .addComponent(txtApellidos, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
                            .addComponent(comboFincas, 0, 176, Short.MAX_VALUE)
                            .addComponent(comboJerarquia, 0, 176, Short.MAX_VALUE)))
                    .addComponent(btnAgregarTelefono))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtPseudonimo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtNombres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtApellidos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(comboFincas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(comboJerarquia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btnAgregarTelefono)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Telefonos"));

        listaTelefono.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        listaTelefono.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                listaTelefonoMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(listaTelefono);

        btnRegistrar.setMnemonic('R');
        btnRegistrar.setText("Registrar");
        btnRegistrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegistrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                    .addComponent(btnRegistrar, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
                .addComponent(btnRegistrar))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(141, 141, 141)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
private void btnAgregarTelefonoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarTelefonoActionPerformed
// TODO add your handling code here:
    String telefono=JOptionPane.showInputDialog("Introduzca un numero Telefonico");
    ((DefaultListModel)this.listaTelefono.getModel()).addElement(telefono); 
    
}//GEN-LAST:event_btnAgregarTelefonoActionPerformed

private void MostrarMenu(java.awt.event.MouseEvent e) {
    if(this.PopupMenu.isPopupTrigger(e))
        this.PopupMenu.show(e.getComponent(), e.getX(), e.getY());
}
private void ItemEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ItemEliminarActionPerformed
// TODO add your handling code here:
    try {
        ((DefaultListModel)this.listaTelefono.getModel()).remove(this.listaTelefono.getSelectedIndex());
    }
    catch (Exception e) {}
}//GEN-LAST:event_ItemEliminarActionPerformed

private void listaTelefonoMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listaTelefonoMouseReleased
// TODO add your handling code here:
    this.MostrarMenu(evt);
}//GEN-LAST:event_listaTelefonoMouseReleased

private void btnRegistrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegistrarActionPerformed
// TODO add your handling code here:
    Usuario u=new Usuario();//GEN-LAST:event_btnRegistrarActionPerformed
    u.setPseudonimo(this.txtPseudonimo.getText());
    u.setPass("0000");
    
    
    if (this.OpMandador.isSelected()) {
        Mandador m=new Mandador();
        m.setNombres(this.txtNombres.getText());
        m.setApellidos(this.txtApellidos.getText());
        u.setMandador(m);
    }
    else {
        Gerente g=new Gerente();
        g.setNombres(this.txtNombres.getText());
        g.setApellidos(this.txtApellidos.getText());
        g.setJerarquia((Jerarquia)this.comboJerarquia.getSelectedItem());
        u.setGerente(g);
    }
    int lon=this.listaTelefono.getModel().getSize();
    if (lon>0) {
        String[] telefonos=new String[lon];
        for (int i=0;i<lon;i++) {
            telefonos[i]=(String)this.listaTelefono.getModel().getElementAt(i);
        }
        u.setTelefonos(telefonos);
    }
    u.Crear();
    this.txtPseudonimo.setText("");
    this.txtNombres.setText("");
    this.txtApellidos.setText("");
    
    ((DefaultListModel)this.listaTelefono.getModel()).removeAllElements();
    
}
    
    /**
     * @param args the command line arguments
     */

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem ItemEliminar;
    private javax.swing.JRadioButton OpGerente;
    private javax.swing.JRadioButton OpMandador;
    private javax.swing.JPopupMenu PopupMenu;
    private javax.swing.JButton btnAgregarTelefono;
    private javax.swing.JButton btnRegistrar;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox comboFincas;
    private javax.swing.JComboBox comboJerarquia;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList listaTelefono;
    private javax.swing.JTextField txtApellidos;
    private javax.swing.JTextField txtNombres;
    private javax.swing.JTextField txtPseudonimo;
    // End of variables declaration//GEN-END:variables
    
}
